package com.mls.fivedistrict.controller.portal;

;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.NewsFlash;
import com.mls.fivedistrict.server.NewsFlashService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

/**
 * @author chenhua
 * @date 2018/6/5 15:33
 */
@RestController
@RequestMapping("/api/app")
public class NewsFlashController extends BaseController {
    @Autowired
    NewsFlashService newsFlashService;

    /**
     * 分页查询列表
     * @return
     */
    @GetMapping("/v1/newsFlashes")
    public ServiceResponse getNewsFlashList(){
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
        List list = newsFlashService.selectList(pageFilter.getWrapper());
        PageInfo<NewsFlash> pageInfo = new PageInfo<>(list, 5);
        return ServiceResponse.createSuccessResponse(pageInfo);
    }

    /**
     * 单个快讯详情
     * @param newsFlashId
     * @return
     */
    @GetMapping("/v1/newsFlash/{newsFlashId}")
    public ServiceResponse getNewsFlashDetail(@PathVariable Long newsFlashId){
        Optional.ofNullable(newsFlashId).orElseThrow(() -> new CustomerException("要查询的快讯id不能为空!"));
        NewsFlash newsFlash = newsFlashService.selectById(newsFlashId);
        if(null!=newsFlash){
            return ServiceResponse.createSuccessResponse(newsFlash);
        }else{
            return ServiceResponse.createErrorResponse("查无此快讯!");
        }
    }
}
